// SPDX-License-Identifier: GPL-2.0
/*
 * USER PROCESS
 *
 * (C) 2024.05.01 BuddyZhang1 <buddy.zhang@aliyun.com>
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>

#define MAP_SIZE	0x1000
#define HEAP_SIZE	0x20

/* BSS/DATA SECTION */
static int a, b = 10;
int c, d = 20;

int main(int argc, char **argv)
{
	void *base, *heap; /* HEAP */
	char stack[32]; /* STACK */
	void *mem; /* MMAP */

	/* ALLOC MEMORY FROM MMAP AREA */
	mem = mmap(NULL, MAP_SIZE,
		   PROT_READ | PROT_WRITE,
		   MAP_ANONYMOUS | MAP_PRIVATE,
		   -1,
		   0);
	if (mem == MAP_FAILED) {
		printf("ERROR: ALLOC MEMORY FAILED.\n");
		return -1;
	}

	/* ALLOC MEMORY FROM HEAP */
	base = sbrk(0);
	heap = base + HEAP_SIZE;
	if (brk(heap) != 0) {
		printf("ERROR: BRK FAILED\n");
		exit(1);
	}

	/* ACCESS */
	sprintf((char *)mem, "Hello BiscuitOS MMAP");
	sprintf((char *)base, "Hello BiscuitOS HEAP");
	sprintf((char *)stack, "Hello BiscuitOS STACK");

	printf("%s %d\n", argv[1], argc);
	printf("%s  %#lx\n%s  %#lx\n%s %#lx\n",
			(char *)mem, (unsigned long)mem,
			(char *)base, (unsigned long)base,
			(char *)stack, (unsigned long)stack);

	/* RECLAIM */
	brk(base);
	munmap(mem, MAP_SIZE);

	return 0;
}
